// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TombstonerRepository.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   TombstonerRepository the currently supported controls for View tombstoning.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace WP7Contrib.View.Controls.Tombstoner
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public static class TombstonerRepository
    {
        private static readonly IDictionary<Type, object> Tombstoners;

        public static ICanTombstone Get(Type type)
        {
            object tombstoner;
            Tombstoners.TryGetValue(type, out tombstoner);

            if (tombstoner == null)
            {
                return null;
            }

            return tombstoner as ICanTombstone;
        }

        public static IEnumerable<ICanTombstone> GetAll()
        {
            return Tombstoners.Values.Cast<ICanTombstone>();
        }

        static TombstonerRepository()
        {
            Tombstoners = new Dictionary<Type, object>();

            var scrollViewer = new ScrollViewerTombstoner();
            Tombstoners.Add(scrollViewer.ControlType, scrollViewer);

            var panorama = new PanoramaTombstoner();
            Tombstoners.Add(panorama.ControlType, panorama);

            var pivot = new PivotTombstoner();
            Tombstoners.Add(pivot.ControlType, pivot);
        }

        public static void RegisterCustom(ICanTombstone tombstoner)
        {
            if (Tombstoners.ContainsKey(tombstoner.ControlType))
            {
                Tombstoners[tombstoner.ControlType] = tombstoner;
            }
            else
            {
                Tombstoners.Add(tombstoner.ControlType, tombstoner);
            }
        }

        public static void UnregisterCustom(ICanTombstone tombstoner)
        {
            if (Tombstoners.ContainsKey(tombstoner.ControlType))
            {
                Tombstoners.Remove(tombstoner.ControlType);
            }
        }

        public static void UnregisterAll()
        {
            Tombstoners.Clear();
        }
    }
}